package print;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.JTable;

import table.models.RankingTable;
import tournamentData.TournamentData;
import core.CustomIcons;

public class PrintableBook extends Book implements Printable {
	Vector < RankingTable > pages;// NB: we assume pages are single
	private final int headerspace = 60;
	private final int footerspace = 90;
	private final int border = 15;
	private PageFormat pageFormat;

	public PrintableBook() {
		super();
		this.pages = new Vector < RankingTable >();
	}

	public void add(RankingTable table, PageFormat pageFormat) {
		this.append(table.getPrintable(JTable.PrintMode.FIT_WIDTH, null, null), pageFormat);
		this.pages.add(table);
	}

	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
		if (pageIndex >= this.pages.size()) {
			return NO_SUCH_PAGE;
		} else {
			try {
				Printable printable = this.pages.elementAt(pageIndex).getPrintable(JTable.PrintMode.FIT_WIDTH, null, null);
				// System.out.println("#############");
				Graphics toPrint = this.setHeaderAndFooter(this.pages.elementAt(pageIndex).getTitle(), graphics, pageFormat, pageIndex);
				// this.printPageFormatZeugs(this.pageFormat);
				int toReturn = printable.print(toPrint, this.pageFormat, 0);
				// System.out.println("#############");
				return toReturn;
			} catch (PrinterException e) {
				e.printStackTrace();
				return NO_SUCH_PAGE;
			}
		}
	}

	private Graphics setHeaderAndFooter(String subTitle, Graphics graphics, PageFormat pageFormat, int pageIndex) {
		/**
		 * set up the page
		 */
		Graphics2D graphics2D = (Graphics2D) graphics;
		int leftmostImageable = (int) pageFormat.getImageableX();
		int topmostImageable = (int) pageFormat.getImageableY();
		int imageableWidth = (int) pageFormat.getImageableWidth();
		int imageableHeight = (int) pageFormat.getImageableHeight();
		if (this.pageFormat == null) {
			this.pageFormat = (PageFormat) pageFormat.clone();
			Paper paper = this.pageFormat.getPaper();
			if (this.pageFormat.getOrientation() == PageFormat.PORTRAIT) {
				paper.setImageableArea(leftmostImageable,
				/* skip header area */
				topmostImageable + this.headerspace, imageableWidth,
				/* reserve space for header and footer */
				imageableHeight - this.footerspace);
			} else {
				paper.setImageableArea(
				/* skip header area */
				topmostImageable + this.headerspace, leftmostImageable,
				/* reserve space for header and footer */
				imageableHeight - this.footerspace, imageableWidth);
			}
			this.pageFormat.setPaper(paper);
		}

		Font font = graphics2D.getFont();

		/**
		 * set up title
		 */

		String title = TournamentData.getInstance().getGeneralTournamentInformation().getTournamentName();
		title += " (" + TournamentData.getInstance().getGeneralTournamentInformation().getStartDate() + ")";
		/* increase size of header */
		graphics2D.setFont(graphics2D.getFont().deriveFont(15f));
		FontMetrics fontMetrics = graphics2D.getFontMetrics();
		graphics2D.drawString(title, leftmostImageable, topmostImageable + this.border);
		// graphics2D.drawString(title, rightmostImageable + ((imageableWidth -
		// fontMetrics.stringWidth(title)) / 2), topmostImageable + 15);

		/**
		 * set up subtitle
		 */

		graphics2D.setFont(font);
		fontMetrics = graphics2D.getFontMetrics();
		graphics2D.drawString(subTitle, leftmostImageable, topmostImageable + 30);

		/**
		 * set up ArComp logo
		 */
		int logoWidth = 100;
		int logoHeight = 50;
		graphics2D.drawImage(CustomIcons.logoImage, (leftmostImageable + imageableWidth) - logoWidth, topmostImageable, logoWidth, logoHeight, Color.WHITE, null);
		// graphics2D.drawRect(leftmostImageable, topmostImageable,
		// imageableWidth, 40);

		/**
		 * set up footer
		 */
		String pageNumber = "Seite " + (pageIndex + 1);
		graphics2D.drawString(pageNumber, leftmostImageable, (topmostImageable + imageableHeight));

		// add printing date
		String DATE_FORMAT_NOW = "dd.MM.yyyy, HH:mm:ss";
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		String date = sdf.format(cal.getTime());
		graphics2D.drawString(date, (leftmostImageable + imageableWidth) - fontMetrics.stringWidth(date), (topmostImageable + imageableHeight));

		// /**
		// * insert the table:
		// */
		// Graphics gCopy = graphics2D.create();
		// // System.out.println("before: " + pageIndex);
		// int retVal = this.tablePrintable.print(gCopy, this.pageFormatJTable,
		// pageIndex);
		// // System.out.println("after: " + pageIndex);
		// gCopy.dispose();
		//
		// return retVal;
		return graphics2D;
	}

	private void printPageFormatZeugs(PageFormat format) {

		System.out.println("" + format.getImageableX() + " " + format.getImageableY() + " " + format.getImageableWidth() + " " + format.getImageableHeight());

	}
}